package example.it.cn;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.*;

import static example.it.cn.TestResourceUtils.qualifiedResource;
import static org.junit.Assert.assertEquals;

import org.junit.Test;

/**
 * @author: Administrator
 * @Date: 上午 11:34 2019-04-21
 * @Description:
 */
public class FactoryBeanTests {

    private static final Class<?> CLASS = FactoryBeanTests.class;
    private static final Resource RETURNS_NULL_CONTEXT = qualifiedResource(CLASS, "returnsNull.xml");


    @Test
    public void testFactoryBeanReturnsNull() throws Exception {
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        new XmlBeanDefinitionReader(factory).loadBeanDefinitions(RETURNS_NULL_CONTEXT);

        assertEquals("null", factory.getBean("factoryBean").toString());
    }


    public static class NullReturningFactoryBean implements FactoryBean<Object> {

        @Override
        public Object getObject() {
            System.out.println("getobject");
            return null;
        }

        @Override
        public Class<?> getObjectType() {
            return null;
        }

        @Override
        public boolean isSingleton() {
            return true;
        }
    }
}
